package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class YangtseNewsAnalyse implements AnalyseNews {

	//http://www.yangtse.com/system/2013/05/07/017157418.shtml
	//http://auto.yzwb.net/qchq/201203/t20120310_839868.htm
	private static final String[] regex={
		"http://.*.yangtse.com/system/[0-9]*/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://.*.yzwb.net/[a-z]*/[0-9]*/t[0-9]*_[0-9]*.htm",
		"http://.*.yangtse.com/.*/[0-9]+-[0-9]+-[0-9]+/[0-9]+.html",
		"http://[a-z]+.yangtse.com/[a-z]+/[0-9\\/]+/[0-9]+.html",
		"http://photo.yangtse.com/[a-z]+/[0-9]+.html"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String author =null;
		title = doc.select("div#content_title").text();
		if(StringUtils.isBlank(title))
			title=doc.select("h5").text();	
		
		date=DateUtils.matchDate(doc.select("div#time").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("h6").text());
		}
		
		doc.select("p:contains(上一篇:),p:contains(下一篇:),.jiathis_style").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#article"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.nr_text"));
		}
		
		author=doc.select("div#ptime a").text();
		if(StringUtils.isBlank(author))
			author=JSoupUtils.matchAuthor(doc, "来源：");
		if (StringUtils.isBlank(title)||date == null) {
		}
		NewsMeta news=new NewsMeta();
		news.setTitle(title);
		news.setDate(date);
		news.setContent(content);
		news.setUrl(url);
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		String url="http://www.yangtse.com/yushijie/2015-11-13/703348.html";
		YangtseNewsAnalyse test=new YangtseNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(test.isDetailPage(url));
		System.out.println(url);
//		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
